package bookshare;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Katharina
 */

import java.sql.*;

public class SQLquery {
    
    private String url = "jdbc:mysql://localhost:3306/bookshare";
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    
    public SQLquery() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        connect();
    }
    
    @Override
    protected void finalize() throws SQLException, Throwable {
        stmt.close();
        rs.close();
        con.close();
        super.finalize();
    }
    
    private void connect() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url,"root","");
            stmt = con.createStatement();
    }
    
    /**
     * für SELECT Statements, die ein RestultSet Objekt returnen
     * @param query SQL-Statement
     * @return
     * @throws SQLException 
     */
    public ResultSet selectQuery(String query) throws SQLException {
        rs = stmt.executeQuery(query);
        return rs;
    }
    
    /**
     * für INSERT, UPDATE oder DELETE Statements
     * @param query SQL-Statement
     * @throws SQLException 
     */
    public void executeUpdate(String query) throws SQLException {
        int res = stmt.executeUpdate(query);
    }
}